package com.yjy.fuyao.stu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yjy.fuyao.stu.entity.CourseCollect;
import com.yjy.fuyao.stu.mapper.CourseCollectMapper;
import com.yjy.fuyao.stu.service.CourseCollectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 课程收藏 服务实现类
 * </p>
 *
 * @author yjy
 * @since 2022-02-26
 */
@Service
public class CourseCollectServiceImpl extends ServiceImpl<CourseCollectMapper, CourseCollect> implements CourseCollectService {

    @Override
    public boolean isStarCourse(String userId, String courseId) {
        LambdaQueryWrapper<CourseCollect> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .eq(CourseCollect::getCourseId,courseId)
                .eq(CourseCollect::getUserId,userId);
        Integer count = baseMapper.selectCount(queryWrapper);
        return count > 0;
    }

    @Override
    public boolean deleteCollect(String userId, String courseId) {
        LambdaUpdateWrapper<CourseCollect> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper
                .eq(CourseCollect::getCourseId,courseId)
                .eq(CourseCollect::getUserId,userId);
        int delete = baseMapper.delete(updateWrapper);
        return true;
    }

    @Override
    public boolean addCollect(String courseId, String userId) {
        LambdaUpdateWrapper<CourseCollect> updateWrapper = new LambdaUpdateWrapper<>();
        CourseCollect courseCollect = new CourseCollect();
        courseCollect.setCourseId(courseId);
        courseCollect.setUserId(userId);
        baseMapper.insert(courseCollect);
        return true;
    }
}
